package com.lottery.im.module.tim.message;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import com.lottery.im.MyApplication;
import com.lottery.im.R;
import com.lottery.im.module.chat.adapter.ChatAdapter;
import com.lottery.im.module.tim.utils.TimFileUtil;
import com.lottery.im.util.DisplayHelper;
import com.lottery.im.widget.shapeimageview.CustomShapeImageView;
import com.tencent.imsdk.TIMCallBack;
import com.tencent.imsdk.TIMGroupMemberInfo;
import com.tencent.imsdk.TIMImage;
import com.tencent.imsdk.TIMImageElem;
import com.tencent.imsdk.TIMImageType;
import com.tencent.imsdk.TIMMessage;
import java.io.IOException;

/**
 * 图片消息数据
 */
public class ImageMessage extends Message {

  private static final String TAG = "ImageMessage";
  private boolean isDownloading;

  public ImageMessage(TIMMessage message) {
    this.message = message;
  }

  public ImageMessage(String path) {
    this(path, false);
  }

  /**
   * 图片消息构造函数
   *
   * @param path 图片路径
   * @param isOri 是否原图发送
   */
  public ImageMessage(String path, boolean isOri) {
    message = new TIMMessage();
    TIMImageElem elem = new TIMImageElem();
    elem.setPath(path);
    elem.setLevel(isOri ? 0 : 1);
    message.addElement(elem);
  }

  /**
   * 显示消息
   *
   * @param viewHolder 界面样式
   * @param context 显示消息的上下文
   */
  @Override
  public void showMessage(final ChatAdapter.ViewHolder viewHolder, final Context context) {
    clearView(viewHolder);
    if (checkRevoke(viewHolder)) return;
    TIMImageElem e = (TIMImageElem) message.getElement(0);
    switch (message.status()) {
      case SendFail:
      case Sending:
        CustomShapeImageView imageView =
            (CustomShapeImageView) LayoutInflater.from(context)
                .inflate(R.layout.layout_message_image, null);
        imageView.setImageBitmap(getThumb(e.getPath()));
        clearView(viewHolder);
        getBubbleView(viewHolder).addView(imageView);
        break;
      case SendSucc:
        for (final TIMImage image : e.getImageList()) {
          if (image.getType() == TIMImageType.Thumb) {
            final String uuid = image.getUuid();
            if (TimFileUtil.isCacheFileExist(uuid)) {
              showThumb(viewHolder, uuid);
            } else {
              image.getImage(TimFileUtil.getCacheFilePath(uuid), new TIMCallBack() {
                @Override
                public void onError(int code, String desc) {//获取图片失败
                  //错误码code和错误描述desc，可用于定位请求失败原因
                  //错误码code含义请参见错误码表
                }

                @Override
                public void onSuccess() {//成功，参数为图片数据
                  showThumb(viewHolder, uuid);
                }
              });
            }
          }
          if (image.getType() == TIMImageType.Original) {
            getBubbleView(viewHolder).setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                navToImageview(image, context);
              }
            });
          }
          //if (image.getType() == TIMImageType.Original) {
          ////  final String uuid = image.getUuid();
          ////  //                        setImageEvent(viewHolder, uuid,context);
          //  getBubbleView(viewHolder).setOnClickListener(new View.OnClickListener() {
          //    @Override
          //    public void onClick(View v) {
          //      navToImageview(image, context);
          //    }
          //  });
          //}
        }
        break;
    }
    showStatus(viewHolder);
  }

  /**
   * 获取消息摘要
   */
  @Override
  public String getSummary() {
    String str = getRevokeSummary();
    if (str != null) return str;

    StringBuilder result = new StringBuilder();

    TIMGroupMemberInfo senderGroupMemberProfile = message.getSenderGroupMemberProfile();
    if (null != senderGroupMemberProfile) {
      // 说明这是群聊消息
      if (TextUtils.isEmpty(message.getSenderGroupMemberProfile().getNameCard())) {
        // 有自己的群名片
        result.append(message.getSenderProfile().getNickName() + "：");
      } else {
        // 无自己的群名片
        result.append(message.getSenderGroupMemberProfile().getNameCard() + "：");
      }
    }
    result.append(MyApplication.getInstance().getString(R.string.summary_image));

    return result.toString();
  }

  /**
   * 保存消息或消息文件
   */
  @Override
  public void save() {
    final TIMImageElem e = (TIMImageElem) message.getElement(0);
    for (TIMImage image : e.getImageList()) {
      if (image.getType() == TIMImageType.Original) {
        final String uuid = image.getUuid();
        if (TimFileUtil.isCacheFileExist(uuid + ".jpg")) {
          //Toast.makeText(MyApplication.getInstance(),
          //    MyApplication.getInstance().getString(R.string.save_exist),
          //    Toast.LENGTH_SHORT).show();
          return;
        }
        image.getImage(TimFileUtil.getCacheFilePath(uuid + ".jpg"), new TIMCallBack() {
          @Override
          public void onError(int i, String s) {
          }

          @Override
          public void onSuccess() {
            //Toast.makeText(MyApplication.getInstance(),
            //    MyApplication.getInstance().getString(R.string.save_success), Toast.LENGTH_SHORT)
            //    .show();
          }
        });
      }
    }
  }

  /**
   * 生成缩略图
   * 缩略图是将原图等比压缩，压缩后宽、高中较小的一个等于198像素
   * 详细信息参见文档
   */
  private Bitmap getThumb(String path) {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);
    int reqWidth, reqHeight, width = options.outWidth, height = options.outHeight;
    int screenWidth = DisplayHelper.getScreenWidth(MyApplication.getInstance());
    if (width > height) {
      reqWidth = 198;
      reqHeight = (reqWidth * height) / width;
    } else {
      reqHeight = 198;
      reqWidth = (width * reqHeight) / height;
    }
    int inSampleSize = 1;
    if (height > reqHeight || width > reqWidth) {
      final int halfHeight = height / 2;
      final int halfWidth = width / 2;
      while ((halfHeight / inSampleSize) > reqHeight
          && (halfWidth / inSampleSize) > reqWidth) {
        inSampleSize *= 2;
      }
    }
    try {
      options.inSampleSize = inSampleSize;
      options.inJustDecodeBounds = false;
      Matrix mat = new Matrix();
      Bitmap bitmap = BitmapFactory.decodeFile(path, options);
      ExifInterface ei = new ExifInterface(path);
      int orientation =
          ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
      switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
          mat.postRotate(90);
          break;
        case ExifInterface.ORIENTATION_ROTATE_180:
          mat.postRotate(180);
          break;
      }
      return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), mat, true);
    } catch (IOException e) {
      return null;
    }
  }

  private void showThumb(final ChatAdapter.ViewHolder viewHolder, String filename) {
    Bitmap bitmap = BitmapFactory.decodeFile(TimFileUtil.getCacheFilePath(filename));
    CustomShapeImageView imageView =
        (CustomShapeImageView) LayoutInflater.from(MyApplication.getInstance())
            .inflate(R.layout.layout_message_image, null);
    imageView.setImageBitmap(bitmap);
    getBubbleView(viewHolder).addView(imageView);
  }

  private void setImageEvent(final ChatAdapter.ViewHolder viewHolder, final String fileName,
      final Context context) {
    getBubbleView(viewHolder).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        //Intent intent = new Intent(context, ImageViewActivity.class);
        //intent.putExtra("filename", fileName);
        //context.startActivity(intent);
      }
    });
  }

  private void navToImageview(final TIMImage image, final Context context) {

    if (null != mOnImageClickListener) mOnImageClickListener.OnImageClick(image.getUrl());

    //ArrayList<String> urls = new ArrayList<>();
    //urls.add(image.getUrl());
    //ImagePreviewActivity.create(context, ImagePreviewActivity.URL_TYPE, 0, urls);

    //if (TimFileUtil.isCacheFileExist(image.getUuid())) {
    //  String path = TimFileUtil.getCacheFilePath(image.getUuid());
    //  File file = new File(path);
    //  if (file.length() < image.getSize()) {
    //    Toast.makeText(context, MyApplication.getInstance().getString(R.string.downloading),
    //        Toast.LENGTH_SHORT).show();
    //    return;
    //  }
    //  //Intent intent = new Intent(context, ImageViewActivity.class);
    //  //intent.putExtra("filename", image.getUuid());
    //  //context.startActivity(intent);
    //} else {
    //  if (!isDownloading) {
    //    isDownloading = true;
    //    image.getImage(TimFileUtil.getCacheFilePath(image.getUuid()), new TIMCallBack() {
    //      @Override
    //      public void onError(int i, String s) {
    //        //错误码code和错误描述desc，可用于定位请求失败原因
    //        //错误码code含义请参见错误码表
    //        Log.e(TAG, "getImage failed. code: " + i + " errmsg: " + s);
    //        Toast.makeText(context, MyApplication.getInstance().getString(R.string.download_fail),
    //            Toast.LENGTH_SHORT).show();
    //        isDownloading = false;
    //      }
    //
    //      @Override
    //      public void onSuccess() {
    //        isDownloading = false;
    //        //Intent intent = new Intent(context, ImageViewActivity.class);
    //        //intent.putExtra("filename", image.getUuid());
    //        //context.startActivity(intent);
    //      }
    //    });
    //  } else {
    //    Toast.makeText(context, MyApplication.getInstance().getString(R.string.downloading),
    //        Toast.LENGTH_SHORT).show();
    //  }
    //}
  }
}